package com.dayuanit.dy16.ebook.ebookweb.controller;

import com.dayuanit.dy16.ebook.ebookweb.dto.ResponseDTO;
import com.dayuanit.dy16.ebook.ebookweb.service.INavService;
import com.dayuanit.dy16.ebook.ebookweb.service.IOrderService;
import com.dayuanit.dy16.ebook.ebookweb.vo.AddressRequestVO;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@RestController
public class OrderController extends BaseController implements InitializingBean {

    @Autowired
    private IOrderService orderService;

    @Autowired
    private INavService navService;

    public OrderController() {
        System.out.println(">>>>>>>>>>>>>>OrderController()");
//        System.out.println(orderService.getClass().getName());//空指针异常，此时还没有注入属性呢！
    }

    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println(">>>>>>>>>>>>>>>>>>afterPropertiesSet()");
        System.out.println(orderService.getClass().getName());
        System.out.println(navService.getClass().getName());
    }

    @RequestMapping("/order/loadMyAddress")
    public ResponseDTO loadMyAddress() {
        return ResponseDTO.success(orderService.loadAddress(getUserId()));
    }

    @RequestMapping("/order/loadArea")
    public ResponseDTO loadArea(int level, int parentId) {
        return ResponseDTO.success(orderService.loadArea(level, parentId));
    }

    @RequestMapping("/order/saveAddress")
    public ResponseDTO saveAddress(AddressRequestVO addressRequestVO) {
        orderService.saveAddress(addressRequestVO, getUserId());
        return ResponseDTO.success();
    }

    @RequestMapping("/order/loadCartInfo")
    public ResponseDTO loadCartInfo() {
        return ResponseDTO.success(orderService.loadCartInfo(getUserId()));
    }

    @RequestMapping("/order/createOrder")
    public ResponseDTO createOrder(int payChannel, long addressId) {
        return ResponseDTO.success(orderService.createOrder(getUserId(), addressId, payChannel));
    }

    @RequestMapping("/pay/alipayNotify")
    public void alipayNotify(HttpServletRequest request, HttpServletResponse response) {

        String total_amount = request.getParameter("username");
        String[] usernames = request.getParameterValues("username");

        Map<String, String[]> parameterMap = request.getParameterMap();

        System.out.println(">>>>>>>>>>>>alipay notify...");

        Map<String, String> notifyParameter = new HashMap<>(parameterMap.size());
        for (Map.Entry<String, String[]> entry :  parameterMap.entrySet()) {
            notifyParameter.put(entry.getKey(), entry.getValue()[0]);
        }

        notifyParameter.forEach((k, v) -> System.out.println(k + " " + v));


        try {
            orderService.alipayNotify(notifyParameter);

            ServletOutputStream outputStream = response.getOutputStream();
            outputStream.write("success".getBytes());
            outputStream.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    @RequestMapping(value = "/order/loadMyOrder")
    public ResponseDTO loadMyOrder(int status) {
//        return ResponseDTO.success(orderService.loadOrder(getUserId(), status));
//        return ResponseDTO.success(orderService.loadOrder2(getUserId(), status));
        return ResponseDTO.success(orderService.loadOrder3(getUserId(), status));
    }


}